package com.qianbao.qianbaobusiness.ui.qrcode;


import android.text.TextUtils;

import com.androidbase.base.mvp.RxMvpPresenter;
import com.androidbase.download.DownloadException;
import com.androidbase.download.DownloadManager;
import com.androidbase.download.downinterfaceimpl.SimpleDownLoadCallBack;
import com.androidbase.utils.RxUtils;
import com.baseandroid.config.MyApi;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.retrofit.json.Data;
import com.qianbao.qianbaobusiness.json.MerchantQrCodeBean;
import com.qianbao.qianbaobusiness.json.QrBean;
import com.qianbao.qianbaobusiness.json.SaveQrcodeBean;

import java.io.File;
import java.util.HashMap;

import javax.inject.Inject;

/**
 * Created by hepeng on 2018/6/22.
 */

public class BindQrCodePresenter extends RxMvpPresenter<BindQrCodeView> {
    MyApi myApi;

    @Inject
    public BindQrCodePresenter(MyApi myApi) {
        this.myApi = myApi;
    }

    public void findMerchantQrcodeList(String posMerchantNo, String merchantNo) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("posMerchantNo", posMerchantNo);
        hashMap.put("merchantNo", merchantNo);
        myApi.findMerchantQrcodeList(hashMap)
                .compose(RxUtils.<Data<MerchantQrCodeBean>>applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<MerchantQrCodeBean>>() {
                    @Override
                    public void onNext(Data<MerchantQrCodeBean> merchantQrCodeBeanData) {
                        if (checkJsonCode(merchantQrCodeBeanData, true)) {
                            getView().updateUI(merchantQrCodeBeanData.getResult());
                        }
                    }
                });
    }

    public void requestDownload(String sUpdateUrl, String downLoadApkFolde, String downLoadApkFileName, String qrcode, String merchantNo) {
        if (TextUtils.isEmpty(sUpdateUrl)) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("type", "1");
            hashMap.put("qrcode", qrcode);
            hashMap.put("merchantNo", merchantNo);
            myApi.saveQrcode(hashMap)
                    .compose(RxUtils.<Data<SaveQrcodeBean>>applySchedulersLifeCycle(getView()))
                    .subscribe(new RxObserver<Data<SaveQrcodeBean>>() {
                        @Override
                        public void onNext(Data<SaveQrcodeBean> saveQrcodeBeanData) {
                            if (checkJsonCode(saveQrcodeBeanData, true)) {
                                getView().updateUrl(saveQrcodeBeanData.getResult().actualUrl);
                                DownloadManager.getInstance()
                                        .download(saveQrcodeBeanData.getResult().actualUrl, new File(downLoadApkFolde), downLoadApkFileName, false, new SimpleDownLoadCallBack() {

                                            @Override
                                            public void onProgress(long finished, long total, int progress) {
                                                if (getView() != null) {
                                                    getView().downloadProgress(progress);
                                                }
                                            }

                                            @Override
                                            public void onCompleted(File downloadfile) {
                                                if (getView() != null) {
                                                    getView().downloadCompleted();
                                                }
                                            }

                                            @Override
                                            public void onFailed(DownloadException e) {
                                                if (getView() != null) {
                                                    getView().downloadFailed();
                                                }
                                            }
                                        });
                            }
                        }
                    });

        } else {
            DownloadManager.getInstance()
                    .download(sUpdateUrl, new File(downLoadApkFolde), downLoadApkFileName, false, new SimpleDownLoadCallBack() {

                        @Override
                        public void onProgress(long finished, long total, int progress) {
                            if (getView() != null) {
                                getView().downloadProgress(progress);
                            }
                        }

                        @Override
                        public void onCompleted(File downloadfile) {
                            if (getView() != null) {
                                getView().downloadCompleted();
                            }
                        }

                        @Override
                        public void onFailed(DownloadException e) {
                            if (getView() != null) {
                                getView().downloadFailed();
                            }
                        }
                    });
        }

    }

    public void merchantQrcodeRemoveBind(QrBean bean) {
        myApi.merchantQrcodeRemoveBind(bean.id)
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data>() {
                    @Override
                    public void onNext(Data data) {
                        if (RxObserver.checkJsonCode(data, true)) {
                            getView().notifyAdapter(bean);
                        }
                    }
                });
    }

    public void merchantQrcodeBind(String merchantNo, String contents) {
        myApi.merchantQrcodeBind(merchantNo, contents)
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data>() {
                    @Override
                    public void onNext(Data stringData) {
                        if (RxObserver.checkJsonCode(stringData, true)) {
                            getView().bindQrCodeSuccess();
                        }
                    }
                });
    }
}
